clear
capture 
clear matrix
mat drop _all
set more off
set matsize 1000
************************************************************************************************************
*****this estimates income process for high and low income households******

global data	"C:/Users/tbold/Dropbox/Tessa and Tobi/JEEA Replication/Data"
global programmes "C:/Users/tbold/Dropbox/Tessa and Tobi//JEEA Replication/Programmes"

*global data "C:\Users\tbroer\Dropbox\Research\Current_Projects\tessa\Data"

global input 	"$data/Input"
global output 	"$data/Output"
global dofiles 	"$data/Dofiles"

cd 	"$data"


tempfile temp1 temp2 temp3



use "allest", clear

*******************************************
****1) transform data (logs, diff, lag)****
*******************************************

xtset HID YEAR
foreach var of varlist AEQCONS AEQINC {
gen ln`var'=ln(`var')
gen ln`var'lag=l.ln`var'
gen diffln`var'=d.ln`var'
}

gen CONS=AEQCONS*MAEQSIZE
gen INC=AEQINC*MAEQSIZE


******************************************
*****INCOME HETEROGENEITY VILLAGE 1*******
******************************************
keep if VILLAGE==1
by HID, sort: egen sd_inc=sd(AEQINC)
by HID, sort: egen mean_inc=mean(AEQINC)
su AEQINC if VILLAGE==1, detail
local inc_med=`r(p50)'
local inc_mean=`r(mean)'


gen above_income=mean_inc>`inc_med' 

matrix define lcvcv_fe_cond=J(4,2,0)


***
local i=0
forvalues j = 0(1)1 {
local i=`i'+1
xi: xtreg lnAEQINC lnAEQINClag i.YEAR if VILLAGE==1 & above_income==`j', i(HID) fe cluster(HID)
gen sample=e(sample)
**and the variance of the alpha_i
predict u_hat, u
su u_hat if YEAR==2 & VILLAGE==1 & above_income==`j', detail
drop u_hat
qui xi: reg lnAEQINC i.HID i.YEAR if VILLAGE==1 & above_income==`j' & sample==1
predict reslhs if sample==1 & above_income==`j', res
qui xi: reg lnAEQINClag  i.HID i.YEAR if VILLAGE==1 & above_income==`j' & sample==1
predict resrhs if sample==1 & above_income==`j', res
reg reslhs resrhs
su resrhs
mat lcvcv_fe_cond[1,`i']=_b[resrhs]*r(Var)
drop reslhs resrhs sample

}



***this calculates the variance after taking out household fixed effects and time dummies

****differences
local i=0
forvalues j = 0(1)1 {
local i=`i'+1
xi: reg d.lnAEQINC i.YEAR if VILLAGE==1 & above_income==`j'
predict res1, res
gen sample1=e(sample)
su res1 if sample1==1 & above_income==`j', detail
matrix lcvcv_fe_cond[2,`i']=r(Var)
drop res1 sample1
} 


*****levels
local i=0
forvalues j = 0(1)1 {
local i=`i'+1
xi: reg lnAEQINC i.HID i.YEAR if VILLAGE==1 & above_income==`j'
predict res1, res
gen sample1=e(sample)
su res1 if sample1==1 & above_income==`j', detail
matrix lcvcv_fe_cond[3,`i']=r(Var)
drop res1 sample1
}


***and the average income, which you also need
local i=0
forvalues j = 0(1)1 {
local i=`i'+1
xi: reg lnAEQINC i.HID i.YEAR if VILLAGE==1 & above_income==`j'
gen sample1=e(sample)
su AEQINC if sample1==1 & above_income==`j', detail
matrix lcvcv_fe_cond[4,`i']=r(mean)
drop sample1
}


drop _all

svmat lcvcv_fe_cond, names(armoments_fe_cond)
outsheet armoments_fe_cond* using "$programmes/armoments_het_fe0", nonames replace
outsheet armoments_fe_cond* using "$output/armoments_het_fe0", nonames replace



 








